package net.gyapp.markim.module.collect.adapter;

import android.text.TextUtils;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import net.gyapp.markim.R;
import net.gyapp.markim.module.basic.base.BaseRecyclerHolder;
import net.gyapp.markim.module.collect.vo.CollectionVo;
import net.gyapp.markim.module.contact.adapter.ContactsAdapter;
import net.gyapp.markim.module.contact.vo.ContactHeaderEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:  winds
 * Data:    2018/8/22
 * Version: 1.0
 * Desc:
 */


public class CollectAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseRecyclerHolder> {

    public final static int TYPE_CONTACT_HEADER = 3001;
    public final static int TYPE_CONTACT_USER = 3002;
    public final static int TYPE_CONTACT_GROUP = 3003;

    public CollectAdapter() {
        super(null);
        addItemType(TYPE_CONTACT_HEADER, R.layout.item_contact_cate);
        addItemType(TYPE_CONTACT_USER, R.layout.item_contact_content);
        addItemType(TYPE_CONTACT_GROUP, R.layout.item_contact_content);
    }


    public void setAdapterData(CollectionVo dataVo) {
        mData.clear();
        if (dataVo.userCollections != null && dataVo.userCollections.size() > 0) {
            List<CollectionVo.UserCollectionsBean> userList = new ArrayList<>();
            List<CollectionVo.UserCollectionsBean> groupList = new ArrayList<>();

            for (int i = 0; i < dataVo.userCollections.size(); i++) {
                CollectionVo.UserCollectionsBean bean = dataVo.userCollections.get(i);
                switch (bean.getItemType()) {
                    case TYPE_CONTACT_USER:
                        userList.add(bean);
                        break;
                    case TYPE_CONTACT_GROUP:
                        groupList.add(bean);
                        break;
                }
            }

            if (groupList.size() > 0) {
                mData.add(new ContactHeaderEntity("群聊", ContactsAdapter.TYPE_CONTACT_HEADER));
                mData.addAll(groupList);
            }

            if (userList.size() > 0) {
                mData.add(new ContactHeaderEntity("好友", ContactsAdapter.TYPE_CONTACT_HEADER));
                mData.addAll(userList);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    protected void convert(BaseRecyclerHolder helper, MultiItemEntity item) {
        switch (item.getItemType()) {
            case TYPE_CONTACT_HEADER:
                convertHeader(helper, (ContactHeaderEntity) item);
                break;
            case TYPE_CONTACT_USER:
                convertUser(helper, (CollectionVo.UserCollectionsBean) item);
                break;
            case TYPE_CONTACT_GROUP:
                convertGroup(helper, (CollectionVo.UserCollectionsBean) item);
                break;
        }
    }

    private void convertUser(BaseRecyclerHolder helper, CollectionVo.UserCollectionsBean item) {
        if(item.userInfo != null) {
            helper.setText(R.id.tv_name, TextUtils.isEmpty(item.userInfo.nick) ? item.userInfo.userName : item.userInfo.nick);
            helper.setImageManager(mContext, R.id.iv_header, item.userInfo.userAvatar, R.drawable.placeholder_group_header);
        }else {
            helper.setText(R.id.tv_name, "用户");
            helper.setImageResource(R.id.iv_header, R.drawable.placeholder_header);
        }
    }

    private void convertGroup(BaseRecyclerHolder helper, CollectionVo.UserCollectionsBean item) {
        if(item.groupInfo != null) {
            helper.setText(R.id.tv_name, item.groupInfo.groupName);
            helper.setImageManager(mContext, R.id.iv_header, item.groupInfo.groupAvatar, R.drawable.placeholder_group_header);
        }else {
            helper.setText(R.id.tv_name, "群聊");
            helper.setImageResource(R.id.iv_header, R.drawable.placeholder_group_header);
        }
    }

    private void convertHeader(BaseRecyclerHolder helper, ContactHeaderEntity item) {
        helper.setText(R.id.tv_cate, item.name);
    }
}
